<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use think\Session;

class Danger extends Base
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        $scoreId = Db::name('people')->where('id',session('user_id'))->value('section_id');
        $res = Db::name('section')
            ->join('y_hospital','y_hospital.id = y_section.hospital_id')
            ->where('y_section.id',$scoreId)
            ->field('y_hospital.name as hospitalName,y_section.name as sectionNmae,y_section.id,y_hospital.id as hid')
            ->find();
        $hospitalId = Db::name('section')->where('id',$scoreId)->value('hospital_id');
        $hospital = Db::name('hospital')->select();
        $section = Db::name('section')->where('hospital_id',$hospitalId)->select();
        $this->assign(['res'=>$res,'section'=>json_encode($section),'id'=>$res['id'],'hospital'=>json_encode($hospital)]);
        return $this->view->fetch();
        return $this->view->fetch();
    }
    public function uploads(){
        $file = request()->file('file');
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($info) {
                $file = $info->getSaveName();
                $res = "/uploads/".$file;
                return $res;
            }
        }
    }
    public function addDanger(){
        $params = $this->request->post();
        $params['createtime'] = time();
        Db::name('people')->where('id',session('user_id'))->update(['section_id'=>$params['section_id']]);
        $res = Db::name('danger')->insert($params);
        return $res?200:400;
    }
}
